package com.qingcheng.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.AdminMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.system.Admin;
<<<<<<< HEAD
import com.qingcheng.pojo.system.AdminRoleList;
import com.qingcheng.service.system.AdminRoleService;
=======
import com.qingcheng.pojo.system.Menu;
>>>>>>> origin/zzh
import com.qingcheng.service.system.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 返回全部记录
     * @return
     */
    public List<Admin> findAll() {
        return adminMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Admin> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Admin> admins = (Page<Admin>) adminMapper.selectAll();
        return new PageResult<Admin>(admins.getTotal(),admins.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Admin> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return adminMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Admin> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Admin> admins = (Page<Admin>) adminMapper.selectByExample(example);
        return new PageResult<Admin>(admins.getTotal(),admins.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
//    public Admin findById(Integer id) {
//        return adminMapper.selectByPrimaryKey(id);
//    }
    public AdminRoleList findById(Integer id) {
        //获取角色列表
        List<Integer> roleList = adminRoleService.findById(id);
        //获取管理员对象
        Admin admin = adminMapper.selectByPrimaryKey(id);
        //封装组合实体类
        AdminRoleList adminRoleList = new AdminRoleList();
        adminRoleList.setAdmin(admin);
        adminRoleList.setRoleIdList(roleList);
        return adminRoleList;
    }



    /**
     * 新增
     * @param adminRoleList
     */
//    public void add(Admin admin) {
//        adminMapper.insert(admin);
//    }
    public void add(AdminRoleList adminRoleList) {
        //获取组合类中的管理员对象,对管理员表进行更新
        Admin admin = adminRoleList.getAdmin();
        if (admin!=null){
            adminMapper.insert(admin);
        }
        System.out.println(admin.getId());
        //取出角色ID集合，循环添加到管理员角色中间表
        List<Integer> roleIdList = adminRoleList.getRoleIdList();
        if(roleIdList!=null && roleIdList.size()>0){
            for (Integer roleId : roleIdList) {
                System.out.println(roleId);
                adminRoleService.add(admin.getId(),roleId);
            }
        }

    }

    /**
     * 修改=删除+添加
     * @param adminRoleList
     */
//    public void update(Admin admin) {
//        adminMapper.updateByPrimaryKeySelective(admin);
//    }
    public void update(AdminRoleList adminRoleList) {
        Admin admin = adminRoleList.getAdmin();
        delete(admin.getId());      //删除管理员角色中间表
        adminMapper.deleteByPrimaryKey(admin.getId());  //删除原先管理员表中的数据

//        adminMapper.insert(admin); //添加修改后的管理员数据到数据库
        add(adminRoleList);        //添加管理员角色中间表
    }

    /**
     *  删除
     * @param adminId
     */
//    public void delete(Integer id) {
//        adminMapper.deleteByPrimaryKey(id);
//    }
    public void delete(Integer adminId) {
        adminRoleService.deleteById(adminId);
        adminMapper.deleteByPrimaryKey(adminId);
    }

    @Override
    public Integer findByname(String name) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",name);
        List<Admin> admins = adminMapper.selectByExample(example);
        if(admins==null){
          throw new RuntimeException("用户不存在");
        }
        return admins.get(0).getId();

    }

    @Override
    public List<Map> findLimitByUsername(String username) {
        List<Admin> adminList = adminMapper.selectAll();
        for (Admin admin : adminList) {
            if (!admin.getLoginName().equals(username)){
                throw new RuntimeException("1,用户不存在");
            }else {
                List<Menu> menuList = adminMapper.findByUsername(username);

                List<Map> menuListByParentId = findMenuListByParentId(menuList, "0");
                return menuListByParentId;
            }
        }


        return null;
    }

    private List<Map> findMenuListByParentId(List<Menu> menuList,String parentId){
        List<Map> mapList = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menu.getParentId().equals(parentId)){
                Map map = new HashMap();
                map.put("path",menu.getId());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                map.put("linkUrl",menu.getUrl());
                map.put("children",findMenuListByParentId(menuList,menu.getId()));
                mapList.add(map);
            }
        }


        return mapList;
    }




    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 用户名
            if(searchMap.get("loginName")!=null && !"".equals(searchMap.get("loginName"))){
                //criteria.andLike("loginName","%"+searchMap.get("loginName")+"%");
                criteria.andEqualTo("loginName",searchMap.get("loginName"));
            }
            // 密码
            if(searchMap.get("password")!=null && !"".equals(searchMap.get("password"))){
                criteria.andLike("password","%"+searchMap.get("password")+"%");
            }
            // 状态
            if(searchMap.get("status")!=null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status",searchMap.get("status"));
            }
            // id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }

        }
        return example;
    }

}
